package qrcode;

/**
 * Created by Administrator on 2016/4/9.
 */

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;


public class QRCode {

    /**
     * 生成图像
     *
     * @throws WriterException
     * @throws IOException
     */
    public void makeQRCode(String url,String fileName) throws WriterException, IOException {
        String filePath = "/usr/temp/qrcode/";
//        Gson gson = new Gson();
//        Map<String,String> map = new HashMap<>();
//        map.put("url",url);
//        String content = gson.toJson(map);

        int width = 200; // 图像宽度
        int height = 200; // 图像高度
        String format = "png";// 图像类型
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(url,
                BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
        Path path = FileSystems.getDefault().getPath(filePath, fileName+".png");
        MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像
        System.out.println("输出成功.");
    }
//    public static void main(String[] arg) throws IOException, WriterException {
//        QRCode qrCode = new QRCode();
//        qrCode.makeQRCode("http://121.42.62.248:8080","1234.png");
//    }

}